// pages/market/good/detail.js
import api from '../../../utils/api'
import message from '../../../utils/message'

// pages/goods/market-detail.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        id: null,
        product_detail: null,
        has_load: false,
        cart_goods_list:[],
        total_price:0,
        market_setting:null,
        nums:1
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if( options.id ){
            this.setData({
                id: options.id
            })

            this.getProductDetail();
            this.getMarketSetting();
            this.getCartProductList();
        }

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

        

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    getProductDetail:function () {
        var me = this;

        var params = {}
        params.id = this.data.id;

        message.show_loading();

        api.product.getProductDetail( params , function (response) {

            message.hide_loading();

            if( response.status==1 ){

                console.log( response.data.detail );

                var product_detail = response.data.detail;
                product_detail.nums =1;

                var comment_list = response.data.comment_list;
                for(var i=0;i<comment_list.length ; i++){
                    comment_list[i].images = JSON.parse( comment_list[i].images );
                }

                var image_list = response.data.product_images;

                try{
                    image_list = JSON.parse(image_list)
                }catch( err ){
                    image_list = [];
                }


                var content = [];

                try{
                  content = JSON.parse( product_detail.content )
                }catch(err){
                  content = [];
                }

                //product_detail.product_images = image_list;
                product_detail.comment_list = comment_list
                product_detail.content = content;

                me.setData({
                    product_detail: product_detail,
                    has_load: true
                })

            }else{
                message.show_error( response );
            }
        })

        /*

        http.doGet("/Product/detail" , params  , function( resp){
            var result = resp.data;
      
            if(result.status==1){
              //检验产品是否出售中， 是否允许购买，是否有错误。
              //渲染评论图片数据为数组元素
              
              var comment_list = result.data.comment_list;
              for(var i=0;i<comment_list.length ; i++){
                comment_list[i].images = JSON.parse( comment_list[i].images );
              }
      
              //商品详情
              var content = [];
              if(result.data.detail.content!=null){
      
                try{
                  content = JSON.parse( result.data.detail.content);
                }catch( error){
                  content = []
                }
                
              }
      
              var product_sku = null;
      
              if(result.data.detail.is_sku==1){
      
                if(result.data.detail.product_skus.length>0){
                  product_sku = result.data.detail.product_skus[0];
                }
              }
             
              //var imageList = JSON.parse(result.data.product_images);
      
              var share_info = null;
              if(result.data.share_info ){
                  share_info = result.data.share_info
              }
              me.setData({
                detail: result.data.detail,
                share_info: share_info,
                imageList: result.data.detail.product_images,
                hasLoad: true,
                comment_list: comment_list,
                content:content,
                product_sku:product_sku,
                retail_user:result.data.retail_user
              })
      
      
              if(result.data.detail.status!=1){
                
                message.show_error("当前商品已下架，快去选购别的商品吧" , function(){
                  wx.navigateBack({
                    delta: 1,
                  })
                });
                return;
              }
            }
      
      
      
        })

        */
        
    },
    goBack:function (params) {
        wx.navigateBack({
          delta: 1,
        })
    },
    getCartProductList:function(){
        var me = this;
        message.show_loading();
        api.shop.getCartGoodList({} , function( response ){
          message.hide_loading();
          if(response.status==1){
              me.setData({
              cart_goods_list: response.data.good_list ,
              total_price:response.data.total_price
              })

              me.updateProductNums( response.data.good_list );
          }else{

          }
        })
    },
    getMarketSetting:function(){
        var me = this;
        api.shop.getShopSetting({} , function(response){
    
          if(response.status==1){
            me.setData({
              market_setting:response.data.shop_setting
            })
    
          }
    
        });
    },
    doAddGood:function (e) {
      var me = this;

      var id =e.currentTarget.dataset.id;
      //var index = e.currentTarget.dataset.index;

      api.shop.addProductToCart( {product_id: id} , function(response){

        if(response.status==1){
          message.toast(response.message);
          me.setData({
            cart_goods_list:response.data.good_list,
            total_price:response.data.total_price
          })

          me.updateProductNums( response.data.good_list );
        }
      });
    },

    doReduceGood:function(e){
      var me = this;
  
      var id = e.currentTarget.dataset.id
  
      api.shop.reduceProductToCart( {product_id:id} , function(response){
  
        if(response.status==1){
          message.toast(response.message);
          me.setData({
            cart_goods_list:response.data.good_list,
            total_price:response.data.total_price
          })
  
          me.updateProductNums( response.data.good_list );
        }
      });
  },
  updateProductNums:function( good_list){

    var product_nums = 1;
    var product_id = this.data.id;

    for(var i=0;i<good_list.length;i++){
      if( good_list[i].product_id==product_id ){
        product_nums = good_list[i].product_nums ;
      }
    }
    this.setData({
      nums:product_nums
    })

  },

  showCartGoods:function(e){
    var action = e.currentTarget.dataset.action;
    if(action==1){
      this.setData({
        is_show_cart_goods:true
      })
    }

    if(action==-1){
      this.setData({
        is_show_cart_goods:false
      })
    }
  },
  doCreateOrder:function(){
    wx.navigateTo({
      url: '/pages/market/market-order-create'
    })
  },
  showPreviewImage:function (e) {

    var url = e.currentTarget.dataset.url;
    if(!url){
      return ;
    }

    var arr = [ url ];

    wx.previewImage({
      urls: arr,
    })
  }



})